package khtn.mylocation;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class LocationFormActivity extends Activity  {
   
	Spinner ddlCategories;
	Context activityContext;
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
        setContentView(R.layout.dataform);
        activityContext = this.getApplication().getApplicationContext();
        Bundle extras = getIntent().getExtras();
        
        ddlCategories = (Spinner)this.findViewById(R.id.ddlCategories);
        BindValues();
        
        Button btnCancel = (Button)this.findViewById(R.id.btnCancel);
        btnCancel.setOnClickListener(onClickCancelButton);
        
        Button btnSave = (Button)this.findViewById(R.id.btnSave);
        btnSave.setOnClickListener(onClickSaveButton);
        
        EditText lngText = (EditText)this.findViewById(R.id.txtLong);
        lngText.setText(Double.toString(extras.getDouble("lng")));
        
        EditText latText = (EditText)this.findViewById(R.id.txtLat);
        latText.setText(Double.toString(extras.getDouble("lat")));
	}
	
	private View.OnClickListener onClickCancelButton = new View.OnClickListener() {
		@Override
		public void onClick(View view) {
			Intent myLocationActivity = new Intent(view.getContext(), MyLocationActivity.class);
            startActivityForResult(myLocationActivity, 0);
		}
	};
	
	private View.OnClickListener onClickSaveButton = new View.OnClickListener() {
		@Override
		public void onClick(View view) {
			EditText name = (EditText)findViewById(R.id.txtNoteName);
			EditText description = (EditText)findViewById(R.id.txtDescription);
			Spinner category = (Spinner)findViewById(R.id.ddlCategories);
			EditText lng = (EditText)findViewById(R.id.txtLong);
			EditText lat = (EditText)findViewById(R.id.txtLat);
			if(name.getText().length() == 0) {
				Toast.makeText(activityContext, "Name is required", Toast.LENGTH_SHORT).show();
				return;
			}
			Location data = new Location(activityContext);
			data.setName(name.getText().toString());
			data.setDescription(description.getText().toString());
			data.setCategory(getPosition(category.getSelectedItem().toString()));
			data.setLng(Double.valueOf(lng.getText().toString()));
			data.setLat(Double.valueOf(lat.getText().toString()));
			data = data.create();
			
			Log.i("data", data.getId().toString());
			Bundle bundle = new Bundle();
            bundle.putInt("setIndexTab", 1);
			Intent myLocationActivity = new Intent(view.getContext(), MyLocationActivity.class);
			myLocationActivity.putExtras(bundle);
            startActivityForResult(myLocationActivity, 0);
		}
	};
	
	private int getPosition(String name) {
		for(int i = 1; i < Helper.CategoryNames.length; i++) {
			if (Helper.CategoryNames[i].equalsIgnoreCase(name)) {
				return i;
			}
		}
		return 1;
	}
	
	private void BindValues() {
		String[] data = new String[Helper.CategoryNames.length - 1];
		for(int i = 1; i < Helper.CategoryNames.length; i++) {
			data[i-1] = Helper.CategoryNames[i];
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(activityContext, R.layout.textview, data); 
		ddlCategories.setAdapter(adapter);
	}
}